function UStorage() {
	var _MAX_STORAGE_ITEM = 10;
	var self = this;
	var KEY_uDataSetList = '_uDataSetList';
	var KEY__uDataClickedDatasetId = '_uDataClickedDatasetId';
	var _storage = window['localStorage'];
	
	this.isSupport = function() {
		if ( _storage ) {
			return true;
		} else {
			return false;
		}
	};
	
	this.set = function(name, value) {
		if ( !self.isSupport() ) {
			return;
		}
		_storage.setItem(name, value);
	};
	
	this.get = function(name) {
		if ( !self.isSupport() ) {
			return;
		}
		return _storage.getItem(name);
	};
	
	this.saveClickedDatasetId = function(id) {
		self.set(KEY__uDataClickedDatasetId, id);
	}
	this.getClickedDatasetId = function() {
		return self.get(KEY__uDataClickedDatasetId);
	}

	this.saveDataSet = function(data) {
		if ( !data || UUtil.isNull(data.inkURL)) {
			return;
		}
		
		var newList = [];
		
		var newDataSet = self.findByInkURL(data.inkURL);
		if ( !newDataSet ) {
			newDataSet = new UDataSet();
			newDataSet.id = new Date().getTime();
			newDataSet.label = data.label;
			newDataSet.inkURL = data.inkURL;
			newDataSet.title = data.title;
			newDataSet.subTitle = data.subTitle;
		}
		newList.push(newDataSet);
		self.set(KEY_uDataSetList + '_' + newDataSet.id,  $.toJSON(data));
		
		var list = self.getDataSetList();
		if ( list  ) {
			for (var i=0; i<list.length; i++) {
				var d = list[i];
				if ( d.inkURL == data.inkURL ) {
					continue;
				}
				newList.push(d);
				if ( newList.length > _MAX_STORAGE_ITEM ) {
					break;
				}
			}
		}
		
		self.set(KEY_uDataSetList,  $.toJSON(newList));
	};
	
	this.getData = function(id) {
		var val = self.get(KEY_uDataSetList + '_' + id);
		if ( UUtil.isNull(val) ) {
			return null;
		}
		
		return $.evalJSON(val);
	}
	
	
	this.getDataSetList = function() {
		var val = self.get(KEY_uDataSetList);
		if ( UUtil.isNull(val) ) {
			return null;
		}
		
		return $.evalJSON(val);
	};	
	
	this.showDataSet = function() {
		var t = '';
		var list = self.getDataSetList();
		if ( !list || list.length < 1  ) {
			t += '<p>';
			if ( self.isSupport() ) {				
				t += '<br/>To add a new data set, click on "Add" link.';
			} else {
				t = 'Your browser does not support offline storage. ';
			}
			t += '<p>';
			$('#uPageOfflineData_content').html(t).trigger("create");
			return;
		} 
		
		t += '<ul  data-role="listview" data-theme="d" data-split-icon="delete" data-inset="true">';
		for (var i=0; i<list.length; i++) {
			var d = list[i];
			t += '<li>';
			t += '<a href="" data-rel="back" onclick="UKey.getInstance().loadSavedData(' + d.id + ');" >';			
			t += d.label;
			
			t += '<br/><br/><p>';
			t += d.inkURL;
			t += '<br/>';
			if ( !UUtil.isNull(d.title)) {
				t += d.title;
			} else {
				t += '<br/>';
			}
			t += '</p>';
			t += '</a>';				
			
			t += '<a href="javascript:UStorage.remove(' + d.id + ');">';
			t += d.inkURL;
			t += '</a>';
			t += '</li>';
		}
		t += '</ul>';
		$('#uPageOfflineData_content').html(t).trigger("create");
	};
	
	this.findById = function(id) {
		var list = self.getDataSetList();
		if ( !list || list.length < 1  ) {
			return null;
		}
		for (var i=0; i<list.length; i++) {
			var d = list[i];
			if ( d.id == id ) {
				return d;
			}
		}
	};
	
	this.findByInkURL = function(url) {
		var list = self.getDataSetList();
		if ( !list || list.length < 1  ) {
			return null;
		}
		for (var i=0; i<list.length; i++) {
			var d = list[i];
			if ( d.inkURL == url ) {
				return d;
			}
		}
	};	
	
	this._remove = function(id) {
		var data = null;
		var list = self.getDataSetList();
		for (var i=0; i<list.length; i++) {
			var d = list[i];
			if ( d.id == id ) {
				data = d;
				break;
			}
		}
		if ( !data ) {
			return;
		}
		var newList = [];
		for (var i=0; i<list.length; i++) {
			var d = list[i];
			if ( d.id == data.id ) {
				continue;
			}
			newList.push(d);
		}
		self.set(KEY_uDataSetList,  $.toJSON(newList));
		self.showDataSet();
	}
	
	this._removeAll = function(id) {
		self.set(KEY_uDataSetList,  null);
	};
};

UStorage.getInstance = function() {
	if ( UStorage._instance == null ) {
		UStorage._instance = new UStorage();
	}
	return UStorage._instance;
};

UStorage.remove = function(id) {
	UStorage.getInstance()._remove(id);
};



function UDataSet() {	
	this.id;
	this.label;
	this.inkURL;
	this.title;
	this.subTitle;
}
